#import <UIKit/UIKit.h>
#import "DevourerAppDelegate.h"
#import "DataType.h"
#import "LayerBuy.h"
#import "IAPManager.h"

static const CGPoint sPosNumber[7] = {
	ccp(80,215), ccp(187,215), ccp(293,215), ccp(400,215), ccp(120,100), ccp(240,100), ccp(360,100)
};

@implementation LayerBuy

-(id)initWithParent:(CCLayer*)parent
{
	if((self = [super init])) {
		mParentLayer = parent;
		// 出现动画
		self.position = ccp(480,0);
		[self runAction:
		 [CCSequence actions:
		  [CCEaseBounceOut actionWithAction:[CCMoveTo actionWithDuration:0.5 position:CGPointZero]],
		  [CCCallFunc actionWithTarget:self selector:@selector(layerLoaded)],NULL]];
		
		// 黑背景
		CCLayerColor* bglayer = [CCLayerColor layerWithColor:ccc4(0,0,0,230)];
		bglayer.position = CGPointZero;
		[self addChild:bglayer];
		
		// background
		CCSprite* bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = gPointCenter;
		bg.opacity = 100;
		[self addChild:bg];
		
		// 标题
		CCLabelTTF* title = [CCLabelTTF labelWithString:NSLocalizedString(@"MoreGold",nil)
											   fontName:gFontName
											   fontSize:30];
		title.position = ccp(20, 300);
		title.anchorPoint = gAnchorLeft;
		title.color = ccGREEN;
		[self addChild:title];
		
		// 金币总数
		mLabelTotalGold = [CCLabelTTF labelWithString:[NSString stringWithFormat:NSLocalizedString(@"Total",nil), commaprint(gGlobalData.gold)]
											 fontName:gFontName
											 fontSize:20];
		mLabelTotalGold.anchorPoint = gAnchorRight;
		mLabelTotalGold.position = ccp(470,300);
		mLabelTotalGold.color = ccGREEN;
		[self addChild:mLabelTotalGold];
		
		// 金币图标
		CCTexture2D* goldIcon = [[CCTextureCache sharedTextureCache] addImage:@"GoldCoin.png"];
		
		CGPoint posIcon[28] = {
			ccp(80,250),
			ccp(177,250),ccp(197,250),
			ccp(283,240),ccp(303,240),ccp(293,260),
			ccp(390,240),ccp(410,240),ccp(390,260),ccp(410,260),
			
			ccp(113,125),ccp(127,125),ccp(108,140),ccp(132,140),ccp(120,150),
			ccp(234,125),ccp(246,125),ccp(226,138),ccp(254,138),ccp(234,150),ccp(246,150),
			ccp(353,125),ccp(367,125),ccp(344,138),ccp(376,138),ccp(353,150),ccp(367,150),ccp(360,138),
		};
		
		for(int i=0; i<28; i++) {
			CCSprite* gold = [CCSprite spriteWithTexture:goldIcon];
			gold.position = posIcon[i];
			[self addChild:gold];
		}
		
		CCMenu* menu = [CCMenu menuWithItems:NULL];
		menu.position = CGPointZero;
		[self addChild:menu];
		
		CCTexture2D* btnNml = [[CCTextureCache sharedTextureCache] addImage:@"btn-02.png"];
		CCTexture2D* btnDown = [[CCTextureCache sharedTextureCache] addImage:@"btn-02-s.png"];
		CCSprite *item, *itemDown;
		CCMenuItemSprite* menuItem;
		CCLabelTTF* menuLabel;

		for(int i=0; i<7; i++) {
			// 金币数量
			CCLabelTTF* labelNumber = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%s G", commaprint(gIapGold[i])]
														 fontName:gFontName
														 fontSize:14];
			labelNumber.position = sPosNumber[i];
			[self addChild:labelNumber];
			// 金币购买按钮
			item = [CCSprite spriteWithTexture:btnNml];
			itemDown = [CCSprite spriteWithTexture:btnDown];
			mMenuItem[i] = [CCMenuItemSprite itemFromNormalSprite:item
												   selectedSprite:itemDown
														   target:self
														 selector:@selector(callMenu:)];
			mMenuItem[i].position = ccp(sPosNumber[i].x,sPosNumber[i].y-25);
			mMenuItem[i].scale = 0.7;
			menuLabel = [CCLabelTTF labelWithString:NSLocalizedString(@"BtnBuy",nil)
										   fontName:gFontName
										   fontSize:24];
			menuLabel.position = ccp(mMenuItem[i].contentSize.width/2,mMenuItem[i].contentSize.height/2+2);
			menuLabel.color = ccGREEN;
			[mMenuItem[i] addChild:menuLabel];
			[menu addChild:mMenuItem[i] z:100+i];
		}
		
		// 返回按钮
		item = [CCSprite spriteWithTexture:btnNml];
		itemDown = [CCSprite spriteWithTexture:btnDown];
		menuItem = [CCMenuItemSprite itemFromNormalSprite:item
										   selectedSprite:itemDown
												   target:self
												 selector:@selector(callMenu:)];
		menuItem.position = ccp(410,30);
		menuLabel = [CCLabelTTF labelWithString:NSLocalizedString(@"BtnClose",nil)
									   fontName:gFontName
									   fontSize:16];
		menuLabel.position = ccp(menuItem.contentSize.width/2,menuItem.contentSize.height/2+2);
		[menuItem addChild:menuLabel];
		[menu addChild:menuItem z:1000];
		
		mNeedUpdateMenu = false;
		
		[self scheduleUpdate];
	}
	return self;
}

-(void)layerLoaded
{
	mParentLayer.visible = false;
}

-(void)update:(ccTime)dt
{
	mLabelTotalGold.string = [NSString stringWithFormat:NSLocalizedString(@"Total",nil), commaprint(gGlobalData.gold)];
	if(mNeedUpdateMenu && !gWaitingIAP) {
		CCLOG(@"Buy: waiting finish");
		DevourerAppDelegate* app = [[UIApplication sharedApplication] delegate];
		[app.activityView stopAnimating];
		for(int i=0; i<7; i++)
			mMenuItem[i].visible = true;
		mNeedUpdateMenu = false;
	}
}

-(void)callMenu:(CCMenuItem*)sender
{
	int id = [sender zOrder];
	
	[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
	
	DevourerAppDelegate* app = [[UIApplication sharedApplication] delegate];

	if(id >= 100 && id <= 106) {
		int idx = id-100;
		[app.activityView setCenter:ccp(sPosNumber[idx].x,320+25-sPosNumber[idx].y)];
		[app.activityView startAnimating];
		mMenuItem[idx].visible = false;
		gWaitingIAP = true;
		mNeedUpdateMenu = true;
		[[IAPManager sharedManager] requestPurchase:idx];
	} else if(id == 1000) {
		[app.activityView stopAnimating];
		mParentLayer.visible = true;
		[self runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.3 position:ccp(580,0)],[CCCallFunc actionWithTarget:self selector:@selector(destroyLayer)],NULL]];
	}
}

-(void)destroyLayer
{
	[mParentLayer onEnter];
	[self.parent removeChild:self cleanup:true];
}

@end
